// pages/searchDetails/searchDetails.js
const api = require("../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: '',
    goodsList: [],
    value: '' //搜索框内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      value: event.detail
    })
    this.getGoods()
  },

  getGoods() {
    wx.request({
      url: api.searchGoods.url,
      method: api.searchGoods.method,
      data: {
        name: this.data.value
      },
      success: res => {
        console.log(res)
        var info = res.data.data
        for (let index = 0; index < info.length; index++) {
          const element = info[index]["img"];
          info[index]["img"] = info[index]["img"].split('|')
        }
        this.setData({
          goodsList: info,
        }, () => {
          console.log(this.data.goodsList)
        })
      }
    })
  },
  onShow(){
    if(this.data.value){
     this.getGoods()
    }
   
  }
})